package com.gwcloud.leetcode.offers;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import com.gwcloud.leetcode.common.datastructure.TreeNode;

/** 
* @ClassName   : Leetcode0320 
* @author      : ganwei 
* @date        : 2020年6月3日 
* @Description : TODO 
*/
public class Leetcode0320 {
	/**
	 * -解题思路，广度优先遍历
	 */
    public int[] levelOrder(TreeNode root) {
    	Queue<TreeNode> q = new LinkedList<TreeNode>();
    	List<Integer> l = new ArrayList<Integer>();
    	if(root == null) {
    		return new int[0];
    	}
    	
    	q.offer(root);
    	
    	while(!q.isEmpty()) {
    		int len = q.size();
    		
    		for(int i = 0; i < len; i++) {
    			TreeNode n = q.poll();
    			l.add(n.val);
    			if(n.left != null) {
    				q.offer(n.left);
    			}
    			if(n.right != null) {
    				q.offer(n.right);
    			}
    		}
    	}
    	int[] res = new int[l.size()];
    	for(int i = 0; i < l.size(); i++) {
    		res[i] = l.get(i);
    	}
    	return res;
    	
    }
    /**
     * -主函数
     */
    public static void main(String[] args) {
    	
    }
}
